home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1538 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: cortex.corpus.uni-muenster.de!news
  2. From: gutschk@uni-muenster.de (Markus Gutschke)
  3. Newsgroups: comp.lang.c,comp.windows.x.motif
  4. Subject: Re: [Q:] flex input from string?
  5. Date: 14 Jan 1996 13:19:08 GMT
  6. Organization: Markus Gutschke, Schlage 5a, 48268 Greven-Gimbte, Germany
  7. Distribution: inet
  8. Message-ID: <GUTSCHK.96Jan14141909@cortex.corpus.uni-muenster.de>
  9. References: <30F660A4.41C6@vmars.tuwien.ac.at>
  10. NNTP-Posting-Host: pppe085.uni-muenster.de
  11. In-reply-to: Joachim Fabini's message of Fri, 12 Jan 1996 14:22:44 +0100
  12.  
  13. In article <30F660A4.41C6@vmars.tuwien.ac.at> Joachim Fabini <jo@vmars.tuwien.ac.at> writes:
  14. >       My question: (f)lex reads by default from the FILE* yyin,
  15. > defaulting to stdin. How can I read input from a _STRING_ ? Say, I get 
  16. > the text field's value and pass it as input to the scanner, that 
  17. > returns some token and I know exactly if the input was correct or not.
  18.  
  19. I would advise you to get hold of a copy of
  20.  
  21.        John R. Levine, Tony Mason, Doug Brown; Lex&Yacc; UNIX
  22.        Programming Tools; O'Reilly&Associates, Inc; Second Edition;
  23.        October 1992; ISBN 1-56592-000-7.
  24.  
  25. This book gives an excellent overview on Lex and Yacc related problems
  26. and summarizes differences between commonly available implementations.
  27.  
  28. It says the following on "Input from Strings" pp. 156:
  29.  
  30. > INPUT FROM STRINGS
  31. > Normally lex reads from a file, but sometimes you want it to read from
  32. > some other source, such as a string in memory. All versions of lex
  33. > make this possible, but the details vary considerably.
  34. > ...
  35. > FLEX
  36. > Although flex provides an input() function, it gets characters using
  37. > optimized in-line code. You can redefine YY_INPUT, the macro it uses
  38. > to read blocks of data. It is called as:
  39. >   YY_INPUT(buffer, result, max_size)
  40. > where buffer is a character buffer, result is a variable in which to
  41. > store the number of characters actually read, and max_size is the size
  42. > of the buffer. To read from a string, have your version of YY_INPUT
  43. > copy data from your string buffer.
  44. > %{
  45. > #undef YY_INPUT
  46. > #define YY_INPUT(b, r, ms) (r = my_yyinput(b, ms))
  47. > %}
  48. > ...
  49. > extern char myinput[];
  50. > extern char *myinputptr; /* current position in myinput */
  51. > extern int myinputlim; /* end of data */
  52. > int my_yyinput(char *buf, int max_size)
  53. > {
  54. >   int n = min(max_size, myinputlim - myinputptr);
  55. >   if (n > 0) {
  56. >     memcpy(buf, myinputptr, n);
  57. >     myinputptr += n;
  58. >   }
  59. >   return n;
  60. > }
  61.  
  62. So, as long as you stick to using Flex, it appears that redefining
  63. YY_INPUT will do the trick. Nonetheless, if you want to release your
  64. code under a license other than GPL, you should be aware of copyright
  65. restriction imposed onto Flex. Thus, you might have to get hold of a
  66. different lexical scanner generator; in this case you should have a
  67. look in "Lex&Yacc" on how you need to modify your code...
  68.  
  69.  
  70. Markus
  71. -- 
  72. Markus Gutschke            Internet: gutschk@math.uni-muenster.de
  73. Schlage 5a
  74. D-48268 Greven-Gimbte
  75. Germany
  76.